package com.hjh.pro.annotation1.annotation;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
public class ConverterFieldAnnotation {

    @Pointcut("@annotation(com.hjh.pro.annotation1.annotation.aspect.ConverterField)")
    private void cut() {
    }

    @Before("cut()")
    public void BeforeCall() {
        System.out.println("事前通知");
    }


    @Around("cut()")
    public Object AroundCall(ProceedingJoinPoint joinPoint) {
        System.out.println("环绕通知之开始");
        Object result = null;
        try {

            printMethodInfo(joinPoint);
            result =joinPoint.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        System.out.println("环绕通知之结束");
        return result;
    }

    @After("cut()")
    public void AfterCall() {
        System.out.println("事后通知");
    }

    private void printMethodInfo(ProceedingJoinPoint pjp ){
        Signature sig = pjp.getSignature();
        MethodSignature msig = null;
        if (!(sig instanceof MethodSignature)) {
            throw new IllegalArgumentException("该注解只能用于方法");
        }
        msig = (MethodSignature) sig;
        Object target = pjp.getTarget();

        try {
            String methodName=msig.getName();
            System.out.println("|调用了方法|methodName:"+methodName);
            System.out.println("  ParameterTypes:"+msig.getParameterTypes());
            Method currentMethod  = target.getClass().getMethod(msig.getName(), msig.getParameterTypes());
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

    }

}